package game.frame;

import com.javaGame.util.MusicUtil;
import game.roles.*;
import game.roles.enemys.Boss;
import game.roles.gamer.Gamer;
import game.roles.gamer.Helicopter;
import game.roles.visual.BackgroundMaps;
import game.utils.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyulai
 * Created with IntelliJ IDEA.
 * Date: 21.5.6
 * Time: 14:23
 * Description: 运行界面
 */
public class MainFrame extends JFrame implements Runnable {
    /**
     * 游戏是否是否运行
     */
    public static boolean flag = true;

    /**
     * 游戏地图
     */
    private BackgroundMaps gameMap = new BackgroundMaps();

    /**
     * 玩家
     */
    public static Gamer gamer = new Gamer();

    public static Helicopter helicopter = new Helicopter();


    /**
     * 敌人
     */
    private Boss boss = new Boss();

    /**
     * 装载对象的集合
     */
    public static List<Role> roleList = new ArrayList<Role>();

    /**
     * 怪物工厂
     */
    private EnemyFactory sf = new EnemyFactory();

    /**
     * 分数
     */
    public static int playerExp = 0;

    /**
     * 玩家生命值
     */
    public static int gamerNum = 10;

    /**
     * 炸弹数量
     */
    public static int boomNum = 5;

    /**
     * 是否通关
     */
    public static Boolean isPass = false;

    /**
     * 内存画笔
     */
    private Image memoryImage;

    /**
     * 内存画布，用于解决多次调用Graphics后产生的闪烁现象
     */
    private Graphics memoryGraphics;

    public static int sp = 0;

    public static MusicUtil ms = new MusicUtil("music/bgm.mp3", true);

    public MainFrame() {
        this.setSize(2000, 600);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        roleList.add(gamer);
        if (StartFrame.isTwoPlayerOrNot) {
            boomNum += 20;
            roleList.add(helicopter);
        }

        //创建和窗体大小一致的内存图片(空白区，与实际图片大小相等
        this.memoryImage = this.createImage(2000, 600);
        //从内存图片中得到画笔，画笔绘制的地点就是内存图片
        this.memoryGraphics = this.memoryImage.getGraphics();

        //给窗体注册键盘监听事件,分为单人与多人的操作监听
        if (!StartFrame.isTwoPlayerOrNot) {
            this.addKeyListener(new PressListenerOne());
        } else {
            this.addKeyListener(new PressListenerTwo());
        }
        //资源加载
        ImageLoad.load(this);
        //产生窗体对象就立即启动一个线程
        new Thread(this).start();
    }


    @Override
    public void run() {
        //每隔20ms进行调用一次画笔刷新，保证画面输出
        while (flag) {
            this.drawFrame(this.getGraphics());
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.dispose();
        //排行榜记录
        if (isPass) {
            FileUtils.init();
            ms.stopMusic();
            new SuccessFrame();
        } else {
            ms.stopMusic();
            new LoseFrame();
        }
    }

    /**
     * 绘制窗体内容
     */
    private void drawFrame(Graphics graphics) {
        //绘制窗口
        this.gameMap.drawFrame(this.memoryGraphics);

        //绘制窗体内所有内容
        sf.creatSoldiers();

        for (int i = 0; i < roleList.size(); i++) {
            Role role = roleList.get(i);
            role.drawRole(this.memoryGraphics);
        }

        this.drawGameInfo();

        graphics.drawImage(this.memoryImage, 0, 0, this);
    }

    /**
     * 进行游戏信息的绘制
     */
    private void drawGameInfo() {
        //设置画笔字体与颜色
        this.memoryGraphics.setColor(Color.WHITE);
        this.memoryGraphics.setFont(new Font("黑体", Font.BOLD, 15));
        this.memoryGraphics.drawString("生命" + gamerNum, 70, 60);
        this.memoryGraphics.drawString("炸弹数量" + MainFrame.boomNum, 170, 60);
        this.memoryGraphics.drawString("玩家分数" + playerExp, 270, 60);
        this.memoryGraphics.drawString("玩家坦克支援" + Gamer.friendlyTankNum, 270, 80);
        this.memoryGraphics.drawString("玩家直升机支援" + Gamer.friendlyHelicopterNum, 270, 100);
        this.memoryGraphics.drawString("已行驶距离" + (0 - BackgroundMaps.mapX), 570, 60);

        this.memoryGraphics.drawRect(570, 80, 210, 10);
        this.memoryGraphics.fillRect(570, 80, (int) ((0 - BackgroundMaps.mapX) * 1.0 / 4200 * 210), 10);


        this.memoryGraphics.drawString("SP", 170, 80);
        this.memoryGraphics.drawRect(200, 66, 50, 10);
        this.memoryGraphics.fillRect(200, 66, (int) (MainFrame.sp * 1.0 / 100 * 50), 10);
    }

    /**
     * 窗体初始化游戏数据x
     */
    protected static void frameInitData() {
        BackgroundMaps.mapX = 0;
        MainFrame.sp = 0;
        MainFrame.playerExp = 0;
        MainFrame.isPass = false;
        MainFrame.flag = true;
        MainFrame.gamerNum = 10;
        MainFrame.boomNum = 10;
        MainFrame.roleList.clear();
        StartFrame.isTwoPlayerOrNot = false;
    }
}